<?php
/**
	接收并，推送数据文件
**/
header('content-type:text/html;charset=utf-8');
require_once($_SITE_PATH_.'m/m_information.php');	
require_once $_SITE_PATH_ . "m/m_tasklist.php";
require_once 'FileFlag_Api.class.php';
require_once 'UpFile_ApiClient.class.php';
require_once 'Queue_Class.php';
//$m_tasklist=new m_tasklist();

$post=isset($_POST)?$_POST:NULL;
$fileflag=new FileFlag_Api();
$response=$fileflag->DoResponse($post);

/* 
$post=$fileflag->get_request_data();
//$fileflag->display(); */

if($response)
	{
		$post=$fileflag->get_request_data();
		$post=json_encode($post);
		$m_tasklist=new m_tasklist();
		$product_id=$post;
		$count=$m_tasklist->getCount(' `product_id`="'.$product_id.'" and `ts_type`="1" and ts_flag="0" and excuflag="0" ');
		
		
		if($count>0)
			{
				$fileflag->display();
				exit;
			}
			elseif($count<=0)
				{
					$count_t=$m_tasklist->getCount(' `product_id`=\''.$product_id.'\' and `ts_type`="1" and excuflag="0" and ts_flag="0" ');
					
					if($count_t<=0)
						{
							$m_tasklist->_ssql = ' insert into tasklist(ts_type,product_id,excuflag,ts_flag) values("1",\''.$product_id.'\',"0","0") ';
							$m_tasklist->query();
						}
					//$count=$m_tasklist->getCount(' `product_id`=\''.$product_id.'\' and `ts_type`="1" and excuflag="0" and ts_flag="0" ');
					
					//新增加代码START
					
					$insert_value=json_decode(trim($product_id,chr(239).chr(187).chr(191)),true);
					$jscount=count($insert_value);
					$incode=array();
					
						if(!empty($insert_value))
							{
								for($i=0;$i<$jscount;$i++)
								{
									if($insert_value)
										{	
												$order_sn=$insert_value['order_sn'];
												$order_address=$insert_value['order_address'];
												$createtime=$insert_value['createtime'];
												
												if(is_array($insert_value['fileinfo']) && !empty($insert_value['fileinfo']))
													{
														for($code_i=0;$code_i<count($insert_value['fileinfo']);$code_i++)
															{
																if(!empty($v))
																	{
																		$code=$insert_value['fileinfo'][$code_i]['code'];
																		$is_upfile=$insert_value['fileinfo'][$code_i]['is_upfile'];
																		$incount=$m_tasklist->getCount(' `order_sn`="'.$order_sn.'" and `is_upfile`="'.$is_upfile.'" and `createtime`="'.$createtime.'" and `order_address`="'.$order_adress.'" and `code`="'.$code.'"');
																		
																		if($incount<=0)
																			{
																				$m_tasklist->_ssql='insert into api_data(order_sn,is_upfile,createtime,order_address,`code`) values("'.$order_sn.'","'.$is_upfile.'","'.$createtime.'","'.$order_adress.'","'.$code.'")';
																				$m_tasklist->query();
																			}
																	}
															}
													}
												
												
										}
								}
							}

					//输出接收结果，然后再执行推送数据
					$fileflag->display();
					exit;
					
					//执行队列任务
					$queue=new QueueClass();
					$m_tasklist->_ssql='select `id`,`product_id` from tasklist where ts_type="1" and excuflag="0" and ts_flag="0" order by `id` ';//asc limit 0,1 
					$product_id=$m_tasklist->getArray($m_tasklist->query());
					//var_dump($product_id);
					$count=$m_tasklist->getCount(' ts_type="1" and excuflag="0" and ts_flag="0" ');
					$code='';
					$code_str='';
					if($count<=0)
						{
							exit;
						}
						else
							{
								for($i=0;$i<$count;$i++)
									{
										//$product_id[$i]['product_id']
										$code=json_decode(trim($product_id[$i]->product_id,chr(231).chr(187).chr(191)),true);//解压JSON数据
										$order_sn='';
										
										$order_sn=$code['order_sn'];
										foreach($code as $key => $value)
											{
												if(strtolower($key)=='fileinfo')
													{
														if(!empty($value) && is_array($value))
															{
																$vcount=count($value);
																for($index=0;$index<$vcount;$index++)
																	{
																		$code_str.=','.$value[$i]['code'];
																	}
															}
													}
											}
										$code_str=trim($code_str,',');
										$product_id[$i]->code=$code_str;
										$product_id[$i]->order_sn=$order_sn;
										$queue->add_item($product_id[$i]->code);
									}
								
								for($i=0;$i<$count;$i++)
									{
										$code=$queue->del_item();
										//$api=new FileFlag_Api();
										$code=number_format($code,'','','');
										$api=new UpFile_ApiClient();
										$auth='tjiePjfjvnyGUuyMFEXVPmsewokCauuZ';
										$data=array('code'=>$code,'order_sn'=>$product_id[$i]->order_sn,'taskid'=>$product_id[$i]->id,'apiflag'=>'1','flag'=>'coprsys','method'=>'copr.update','type'=>'json','timestamp'=>time(),'charset'=>'utf-8','ver'=>1,'file_status'=>'');
										//var_dump($data);
										//exit;
										$url='http://copr.hucai.com/index.php?a=apis.FileFlag_Client';
										$result=$api->DoRequest($url,$auth,$data);
										//var_dump($result);
										usleep(100);
										 
									}
							}
					exit;
				} 
		

		
	}
	else
		{
			$fileflag->display();
			exit;
		}

exit;

?>